//
//  SAEditShopCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/12.
//

#import "SAEditShopCell.h"
#import "NSString+FintCommon.h"

@interface SAEditShopCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *leftLab;
@property (nonatomic, strong) UITextField *rightTf;
@property (nonatomic, strong) UIImageView *allowImageV;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UISwitch *mySwitch;
@property (nonatomic, strong) UILabel *dateLab;
@property (nonatomic, strong) UILabel *percentLab;
@property (nonatomic, strong) UIButton *productCodeBtn;
@property (nonatomic, strong) UIButton *singleCodeBtn;

@end

@implementation SAEditShopCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)refreshLeftTitle:(NSString *)leftTitle RightTitle:(NSString *)rightTitle Type:(NSInteger)type {
    self.leftLab.text = leftTitle;
    self.rightTf.text = rightTitle;
    self.leftLab.textColor = kBlackColor;
    self.rightTf.userInteractionEnabled = YES;
    self.rightTf.placeholder = @"";
    
    self.rightTf.keyboardType = UIKeyboardTypeDecimalPad;
    if (type == 0) {
        self.rightTf.hidden = NO;
        self.allowImageV.hidden = YES;
        self.mySwitch.hidden = YES;
        self.dateLab.hidden = YES;
    }else if (type == 1) {
        self.rightTf.hidden = NO;
        self.allowImageV.hidden = NO;
        self.mySwitch.hidden = YES;
        self.dateLab.hidden = YES;
    }else {
        self.rightTf.hidden = YES;
        self.allowImageV.hidden = YES;
        self.mySwitch.hidden = NO;
        self.dateLab.hidden = NO;
        if ([rightTitle isEqual:@"1"]){
            self.mySwitch.on = YES;
            if ([leftTitle isEqualToString:@"上架"]) {
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSString *oneDayStr = [dateFormatter stringFromDate:[NSDate date]];
                NSString *anotherDayStr = self.date;
                NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
                NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
                NSComparisonResult result = [dateA compare:dateB];
                if (result == NSOrderedDescending) {
                    self.dateLab.text = self.date;
                }else{
                    self.dateLab.text = oneDayStr;
                }
            }else {
                self.dateLab.text = @"";
            }
        }else {
            self.mySwitch.on = NO;
            self.dateLab.text = @"";
        }
    }
    
    _rightTf.clearButtonMode = UITextFieldViewModeNever;
    if  ([leftTitle isEqualToString:@"入库价"] || [leftTitle isEqualToString:@"实体零售价"] || [leftTitle isEqualToString:@"折扣"] || [leftTitle isEqualToString:@"微店价"] || [leftTitle isEqualToString:@"场地"] || [leftTitle isEqualToString:@"经销商"]) {
        self.rightTf.userInteractionEnabled = YES;
        _rightTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    }else {
        self.rightTf.userInteractionEnabled = NO;
    }
    
    if  ([leftTitle isEqualToString:@"颜色"] && rightTitle.length == 0) {
        self.rightTf.placeholder = @"默认均色";
    }
    if  ([leftTitle isEqualToString:@"尺码"] && rightTitle.length == 0) {
        self.rightTf.placeholder = @"默认均码";
    }
    
    //新增折扣和条码功能，在原有基础上添加
    self.productCodeBtn.hidden = YES;
    self.singleCodeBtn.hidden = YES;
    self.percentLab.hidden = YES;
    if ([leftTitle isEqualToString:@"折扣"]) {
        self.percentLab.hidden = NO;
        self.rightTf.frame = SARectMake(128, 0, 220, 40);
        self.rightTf.placeholder = @"80表示8折";
    }else {
        if ([leftTitle isEqualToString:@"商品条码"] || [leftTitle isEqualToString:@"单品条码"]) {
            self.rightTf.frame = SARectMake(128, 0, 200, 40);
            self.rightTf.placeholder = @"不输入条码,系统自动生成";
            if ([leftTitle isEqualToString:@"商品条码"]) {
                self.rightTf.userInteractionEnabled = YES;
                self.rightTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                self.rightTf.clearButtonMode = UITextFieldViewModeWhileEditing;
                self.productCodeBtn.hidden = NO;
            }else {
                self.singleCodeBtn.hidden = NO;
            }
        }else if ([leftTitle isEqualToString:@"实体零售价"] || [leftTitle isEqualToString:@"入库价"] || [leftTitle isEqualToString:@"微店价"]){
            self.rightTf.frame = SARectMake(128, 0, 234, 40);
        }else if ([leftTitle isEqualToString:@"场地"] || [leftTitle isEqualToString:@"经销商"]){
            self.rightTf.frame = SARectMake(128, 0, 215, 40);
            self.rightTf.keyboardType = UIKeyboardTypeDefault;
        }else {
            self.rightTf.frame = SARectMake(128, 0, 215, 40);
        }
    }
    
    NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
    if ([productViewCostPrice isEqualToString:@"1"]) {
        if  ([leftTitle isEqualToString:@"入库价"]) {
            self.leftLab.textColor = kLightgGrayColor;
            self.rightTf.userInteractionEnabled = NO;
            self.rightTf.text = @"******";
        }
    }
}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (string.length == 0) {
        return YES;
    }
    if ([self.leftLab.text isEqualToString:@"商品条码"]) {
        if (textField.text.length > 19) {
            return NO;
        }
    }else if ([self.leftLab.text isEqualToString:@"场地"]) {
        if (textField.text.length > 30) {
            return NO;
        }else {
            return YES;
        }
    }else if ([self.leftLab.text isEqualToString:@"经销商"]) {
        if (textField.text.length > 30) {
            return NO;
        }else {
            return YES;
        }
    }else {
        if (textField.text.length > 7) {
            return NO;
        }
    }
    if ([self.leftLab.text isEqualToString:@"折扣"]) {
        if ([string isEqualToString:@"."]) {
            return NO;
        }
        NSString *newStr = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if ([newStr floatValue] > 100) {
            return NO;
        }
    }
    
    if ([self.leftLab.text isEqualToString:@"商品条码"]) {
        return [self validateNumber:string];
    }
    
    BOOL isHaveDian =NO;
    if([textField.text containsString:@"."]) {
        isHaveDian =YES;
    }
    if(string.length > 0) {
        //当前输入的字符
        unichar single = [string characterAtIndex:0];
        // 不能输入.0-9以外的字符
        if(!((single >='0'&& single <='9') || single =='.')){
            return NO;
        }
        // 只能有一个小数点
        if(isHaveDian && single =='.') {
            return NO;
        }
        // 如果第一位是.则前面加上0.
        if((textField.text.length==0) && (single =='.')) {
            textField.text=@"0";
        }
        // 如果第一位是0则后面必须输入点，否则不能输入。
        if([textField.text hasPrefix:@"0"]) {
            if(textField.text.length>1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1,1)];
                if(![secondStr isEqualToString:@"."]) {
                    return NO;
                }
            }else{
                if(![string isEqualToString:@"."]) {
                    return NO;
                }
            }
        }
        // 小数点后最多能输入两位
        if(isHaveDian) {
            NSRange ran = [textField.text rangeOfString:@"."];
            if(range.location > ran.location) {
                if([textField.text pathExtension].length>1) {
                    return NO;
                }
            }
        }
    }
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678\\9*-"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void)textFieldEditChanged:(UITextField *)textField {
    NSString *newStr = [textField.text noEmoji];
    if (textField.text.length != newStr.length) {
        textField.text = newStr;
    }
    !self.inputBlock ?: self.inputBlock(textField.text);
}

#pragma mark - event
- (void)switchAction:(UISwitch *) s1 {
    if (s1.on == YES) {
        !self.onBlock ?: self.onBlock(@"1");
    }else{
        !self.onBlock ?: self.onBlock(@"0");
    }
}

-(void)clickedProductCodeBtn {
    !self.productCodeBlock ?: self.productCodeBlock();
}

-(void)clickedSingleCodeBtn {
    !self.singleCodeBlock ?: self.singleCodeBlock();
}

#pragma mark - initUI
-(void)initUI {
    [self.contentView addSubview:self.leftLab];
    [self.contentView addSubview:self.rightTf];
    [self.contentView addSubview:self.allowImageV];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.mySwitch];
    [self.contentView addSubview:self.dateLab];
    [self.contentView addSubview:self.percentLab];
    [self.contentView addSubview:self.productCodeBtn];
    [self.contentView addSubview:self.singleCodeBtn];
}

#pragma mark - lazy
-(UILabel *)leftLab {
    if (!_leftLab) {
        _leftLab = [[UILabel alloc] initWithFrame:SARectMake(25, 0, 100, 40)];
        _leftLab.textColor = kBlackColor;
        _leftLab.font = kFont(14);
    }
    return _leftLab;
}

-(UITextField *)rightTf {
    if (!_rightTf) {
        _rightTf = [[UITextField alloc] initWithFrame:SARectMake(128, 0, 215, 40)];
        _rightTf.font = kFont(14);
        _rightTf.textColor = kGrayColor;
        _rightTf.textAlignment = NSTextAlignmentRight;
        _rightTf.keyboardType = UIKeyboardTypeDecimalPad;
        _rightTf.delegate = self;
        [_rightTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _rightTf;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(0, 39, kBaseScreenWidth, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UIImageView *)allowImageV {
    if (!_allowImageV) {
        _allowImageV = [[UIImageView alloc] initWithFrame:SARectMake(355, 13, 7, 13)];
        _allowImageV.image = [UIImage imageNamed:@"shop_grayAllow"];
    }
    return _allowImageV;
}

-(UISwitch *)mySwitch {
    if (!_mySwitch) {
        _mySwitch = [[UISwitch alloc] initWithFrame:SARectMake(0, 0, 0, 0)];
        _mySwitch.y = (kScaleNum(40) - _mySwitch.height)/2;
        _mySwitch.x = kScreenWidth - _mySwitch.width - kScaleNum(15);
        [_mySwitch setOnTintColor:kGreenColor];
        [_mySwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _mySwitch;
}

-(UILabel *)dateLab {
    if (!_dateLab) {
        _dateLab = [[UILabel alloc] initWithFrame:SARectMake(216, 0, 100, 40)];
        _dateLab.textColor = kGrayColor;
        _dateLab.font = kFont(14);
    }
    return _dateLab;
}

-(UILabel *)percentLab {
    if (!_percentLab) {
        _percentLab = [[UILabel alloc] initWithFrame:SARectMake(347, 0, 15, 40)];
        _percentLab.textColor = kBlackColor;
        _percentLab.text = @"%";
        _percentLab.font = kFont(14);
        _percentLab.textAlignment = NSTextAlignmentRight;
    }
    return _percentLab;
}

-(UIButton *)productCodeBtn {
    if (!_productCodeBtn) {
        _productCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _productCodeBtn.frame = SARectMake(335, 0, 36, 40);
        [_productCodeBtn setImage:[UIImage imageNamed:@"gray_san"] forState:UIControlStateNormal];
        [_productCodeBtn addTarget:self action:@selector(clickedProductCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _productCodeBtn;
}

-(UIButton *)singleCodeBtn {
    if (!_singleCodeBtn) {
        _singleCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _singleCodeBtn.frame = SARectMake(330, 0, 36, 40);
        [_singleCodeBtn setTitle:@"设置" forState:UIControlStateNormal];
        _singleCodeBtn.titleLabel.font = kFont(14);
        [_singleCodeBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
        [_singleCodeBtn addTarget:self action:@selector(clickedSingleCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _singleCodeBtn;
}

@end
